<?php

namespace Admin\Controllers;

use Models\Doctypes;
use Models\Topics;
use Forms\DocTypeForm;
use Forms\TopicForm;
use Forms\SearchForm;

class CategoryController extends ControllerBase {

    public function typeAction() {
        $form = new DocTypeForm();
        $types = Doctypes::find();
        $this->view->types = $types;
        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost())) {
                $type = new Doctypes();
                $type->name = $this->request->getPost('name');
                $type->points = 10;
                if ($type->save()) {
                    $form->appendMessage("Thêm dữ liệu thành công", "addtype", null);
                    /* $this->flash->success('Thêm dữ liệu thành công!'); */
                    $this->response->redirect('admin/manage_type');
                } else {
                    foreach ($type->getMessages() as $value) {
                        $form->appendMessage($value, "addtype", null);
                        /* $this->flash->error($type->getMessages()); */
                    }
                }
            }
        }
        $this->view->form = $form;
    }

    public function editTypeAction() {
        $id = $this->request->getPost('id');
        $name = $this->request->getPost('name');
        if ($id && $name) {
            $type = Doctypes::findFirstById($id);
            $type->name = $name;
            if (!$type->save()) {
                echo 'Không thể lưu dữ liệu, dữ liệu không phù hợp!<br>';
            }
        } else {
            echo 'Không thể lưu dữ liệu, dữ liệu không phù hợp!<br>';
        }
        $this->view->disable();
    }

    public function deleteTypeAction() {
        $ids = $this->request->getPost('ids');
        if ($ids) {
            $ids_str = implode(',', $ids);
            $types = Doctypes::find("id IN ($ids_str)");
            foreach ($types as $type) {
                $topics = Topics::find("doctype_id ='$type->id'");
                foreach ($topics as $topic) {
                    $topic->delete();
                }
                $type->delete();
            }
        }
        $this->view->disable();
    }

    public function topicAction() {        
        $form = new TopicForm();
        $topics = Topics::find();
        $types = Doctypes::find();
        $this->view->topics = $topics;
        $this->view->types = $types;
        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost())) {
                $topic = new Topics();
                $topic->name = $this->request->getPost('name');
                $topic->doctype_id = $this->request->getPost('doctype');
                if ($topic->save()) {
                    $form->appendMessage("Thêm dữ liệu thành công", "addtopic", null);
                    $this->response->redirect('admin/manage_topic');
                } else {
                    foreach ($topic->getMessages() as $value) {
                        $form->appendMessage($value, "addtopic", null);                       
                    }
                }
            }
        }
        $this->view->form = $form;
    }

    public function editTopicAction() {
        $id = $this->request->getPost('id');
        $name = $this->request->getPost('name');
        $type = $this->request->getPost('type');
        if ($id && $name && $type) {
            $topic = Topics::findFirstById($id);
            $topic->name = $name;
            $topic->doctype_id = $type;
            if (!$topic->save()) {
                echo 'Không thể lưu dữ liệu, dữ liệu không phù hợp!<br>';
            }
        } else {
            echo 'Không thể lưu dữ liệu, dữ liệu không phù hợp!<br>';
        }
        $this->view->disable();
    }

    public function deleteTopicAction() {
        $ids = $this->request->getPost('ids');
        if ($ids) {
            $ids_str = implode(',', $ids);
            $topics = Topics::find("id IN ($ids_str)");
            foreach ($topics as $topic) {
                $topic->delete();
            }
        }
        $this->view->disable();
    }

}
